package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AboutUs;
import com.wude.bean.ShareInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.ChongActivity;
import com.wude.guild.LoginActivity;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.ExtractTrafficActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.MineMenuAdapter;
import com.wude.guild.app.GuildApplication;
import com.wude.guild.dialog.SharedDialog;
import com.wude.guild.servercore.app.CubeFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.HashMap;

import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/23 0023.
 */
@SuppressLint("HandlerLeak")
public class mineFragment extends CubeFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ImageView icon;
    private TextView nickName;
    private TextView ptb;//平台币
    private TextView tv_jifen;//积分
    private RelativeLayout yidenglu;
    private RelativeLayout weidenglu;
    private ListView menus;
    private ImageView iv_setting;
    private LinearLayout me_core;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.mine_fragment, container, false);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("mineFragment");
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView(view);
    }

    @Override
    public void onResume() {
        super.onResume();
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            weidenglu.setVisibility(View.GONE);
            yidenglu.setVisibility(View.VISIBLE);
            HashMap<String, String> map = new HashMap<>();
            map.put("token", loginUser.token);
            HttpCom.POST(handler, HttpCom.getUserInfoUrl(), map, false);
        } else {
            yidenglu.setVisibility(View.GONE);
            weidenglu.setVisibility(View.VISIBLE);
        }
        MobclickAgent.onPageStart("mineFragment");
    }

    private void initView(View view) {
        icon = findView(view, R.id.icon);
        nickName = findView(view, R.id.nick_name);
        ptb = findView(view, R.id.ptb);
        yidenglu = findView(view, R.id.yidenglu);
        weidenglu = findView(view, R.id.weidenglu);
        menus = findView(view, R.id.menus);
        iv_setting = findView(view, R.id.iv_setting);
        me_core = findView(view, R.id.me_core);
        tv_jifen = findView(view, R.id.tv_jifen);

        MineMenuAdapter adapter = new MineMenuAdapter(getContext());
        menus.setAdapter(adapter);
        menus.setOnItemClickListener(this);

        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            weidenglu.setVisibility(View.GONE);
            yidenglu.setVisibility(View.VISIBLE);
            HashMap<String, String> map = new HashMap<>();
            map.put("token", loginUser.token);
            HttpCom.POST(handler, HttpCom.getUserInfoUrl(), map, false);
        } else {
            yidenglu.setVisibility(View.GONE);
            weidenglu.setVisibility(View.VISIBLE);
        }
        me_core.setOnClickListener(this);
        iv_setting.setOnClickListener(this);
        weidenglu.setOnClickListener(this);
    }

    /**
     * 获取用户信息
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    UserInfo userInfo = HttpUtils.DNSUser(msg.obj.toString());
                    if (userInfo != null) {
                        if (TextUtils.isEmpty(userInfo.nickname)) {
                            nickName.setText(userInfo.account);
                        } else {
                            nickName.setText(userInfo.nickname + "");
                        }
                        ptb.setText(userInfo.balance);
                        tv_jifen.setText(userInfo.jifen);

                        if (userInfo.tou != null && !userInfo.tou.equals("")) {
                            MCUtils.fillImage(icon, userInfo.tou);
                        }
                    } else {
                        ToastUtil.showToast("用户信息过期，重新登录");
                        startActivity(new Intent(getContext(), LoginActivity.class));
                        DbManager db = DbUtils.getDB();
                        try {
                            db.delete(UserInfo.class);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        yidenglu.setVisibility(View.GONE);
                        weidenglu.setVisibility(View.VISIBLE);
                    }
                    break;
                case 2:

                    break;
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            Intent intent = new Intent(getActivity(), MineManagerActivity.class);
            switch (i) {
                case 0://绑币管理  BangbiFragment
                    intent.putExtra("fragmentName", "BangbiFragment");
                    break;
                case 1://账户充值
                    if (GuildApplication.application.isPay()) {
                        intent = new Intent(getActivity(), ChongActivity.class);
                    } else {
                        intent = new Intent(getActivity(), ExtractTrafficActivity.class);
                    }
                    break;
                case 2://我的礼包 MyGiftFragment
                    intent.putExtra("fragmentName", "MyGiftFragment");
                    break;
                case 3://我的积分
                    intent.putExtra("fragmentName", "JifenDetailFragment");
                    break;
                case 4://我的游戏 MyGameFragment
                    intent.putExtra("fragmentName", "MyGameFragment");
                    break;
                case 5://我的收藏 MyCollectionFragment
                    intent.putExtra("fragmentName", "MyCollectionFragment");
                    break;
                case 6://我的评论 MyCommentFragment
                    intent.putExtra("fragmentName", "MyCommentFragment");
                    break;
                case 7://我的流量
                    intent.putExtra("fragmentName", "MyTrafficFragment");
                    break;
                case 8://推荐给好友
                    intent = null;
                    if (Utils.getLoginUser() != null)
                        HttpCom.POST(bhandler, HttpCom.getVisonURL(), null, false);
                    else
                        intent = new Intent(getContext(), LoginActivity.class);
                    break;
                case 9://问题与建议 AdviseFragment
                    intent.putExtra("fragmentName", "AdviseFragment");
                    break;
                case 10://关于我们 AboutUsFragment
                    intent.putExtra("fragmentName", "AboutUsFragment");
                    break;
            }
            if (intent != null) {
                startActivity(intent);
            }
        } else {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    private SharedDialog dialog;
    /**
     * 获取分享内容
     */
    Handler bhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    AboutUs about = HttpUtils.DNSAboutUs(msg.obj.toString());
                    if (about != null) {
                        ShareInfo shareInfo = new ShareInfo();
                        shareInfo.logoUrl = about.iconUrl;
                        shareInfo.shareUrl = about.guanwang;
                        shareInfo.text = about.appname;
                        shareInfo.title = about.appname;
                        if (dialog == null) {
                            dialog = new SharedDialog(getContext(), shareInfo, 1);
                            dialog.show();
                        } else if (dialog != null && !dialog.isShowing()) {
                            dialog.show();
                        }
                    }
                    break;
                case 2:

                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), MineManagerActivity.class);
        switch (view.getId()) {
            case R.id.me_core:          //用户中心
                UserInfo loginUser = Utils.getLoginUser();
                if (loginUser != null) {
                    intent.putExtra("fragmentName", "EditFragment");
                    startActivity(intent);
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.iv_setting:           //设置
                intent.putExtra("fragmentName", "SettingFragment");
                startActivity(intent);
                break;
            case R.id.weidenglu:         //前往登录页面
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
        }
    }
}
